<?php
/**
 * Dao of Customer
 *
 * @author fsqijy
 */


require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class CustomerDao extends BaseDao{

    protected $_name;

    public function  _setup() {
        $this->_name = $this->prefix() . "users";
        parent::_setup();
    }

    public function updateCustomer($data,$id){
         $where = $this->db_w->quoteInto('id = ?', $id);
         $this->updateData($data,$where);
    }

    public function deleteCustomer($id){
    	$isDel = array("is_delete"=>1);
    	$where =  $this->db_w->quoteInto('id = ?', $id);
    	$this->updateData($isDel, $where);
    }

    public function getCustomerByLimit($limit){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("is_delete = 0");
        $select->order("created DESC");
        $select->limit(10);
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function getCustomerByName($name){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("is_delete = 0");
        $select->where("name = ?", $name);
        $sql = $select->__toString();    
        $result = $this->db_r->fetchRow($sql);
        return $result;                	
    }
/*
 * Steve
 */
    public function checkUserByName($name,$id){
    	 $select = $this->db_r->select();
         $select->from($this->_name,"*");
         $select->where("is_delete = 0");
         $select->where("name = ?", $name);
         if($id>0){
         	$select->where("id != ?", $id);
         }
         $sql = $select->__toString();    
         $result = $this->db_r->fetchRow($sql);
        return $result; 
    }
    
    public function checkUserByEmail($email,$id){
    	 $select = $this->db_r->select();
         $select->from($this->_name,"*");
         $select->where("is_delete = 0");
         $select->where("email = ?", $email);
         if($id>0){
         	$select->where("id != ?", $id);
         }
         $sql = $select->__toString();    
         $result = $this->db_r->fetchRow($sql);
        return $result; 
    }
    public function getDeputyByCampusID($campusID,$userID){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("is_delete = 0");
        $select->where("certf = 3");//ambassador
        $select->where("status = 1");//active
        $select->where("campus_id = ?", $campusID);
        $select->where("id != ? ",$userID);
        $sql = $select->__toString();
        return $this->db_r->fetchAll($sql);
    }
    
   public function returnPoints($points,$userID)
   {
   	    $db = $this->db_w;
    	$query = sprintf('update %s set points_buyer=points_buyer+%d where id = %d',$this->_name,$points,$userID);
    	$db->query($query);
   }
   
   public function getCustomersByIds($idStr){
    	$select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("id in ($idStr)");
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function getUsersByPId($parentId){
    	$select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("parent_id = ?",$parentId);
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function getCustomersByCertf($certfStr){
    	$select = $this->db_r->select();
        $select->from($this->_name,"email");
        $select->where("certf in ($certfStr)");
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function getCustomersByRank($rankStr){
    	$select = $this->db_r->select();
        $select->from($this->_name,"email");
        $select->where("rank in ($rankStr)");
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function getRandomInvalidUser()
    {
        $select = $this->db_r->select();
        $select->from($this->_name, "*");
        $select->where("password is null");
        $select->where("is_delete=0");
        $select->where("id <> 2");
        $select->where("id <> 10");
        $select->order("rand()");
        $select->limit(0, 1);
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }
}
